import { OrgModel } from '@/api/manager/organize/interface'
import { IOrgTreeUpdateMt, OrgTreeModel } from './translator'
export const flatOrgTreeToList = (tree: OrgTreeModel[]) => {
	let stack = [...tree] as OrgTreeModel[]
	let result = []
	while (stack.length !== 0) {
		const item = stack.shift()!
		result.push({
			org: item.org,
			id: item.id,
			parentId: item.parentId,
			status: item.status,
			nestingLevel: item.nestingLevel,
			children: item.children,
		})
		if (item.status.isOpen) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift({
					...item.children[i],
					nestingLevel: item.nestingLevel + 1,
				})
			}
		}
	}
	return result
}
export function getItemInOrgTree(tree: OrgTreeModel[], callback: (item: OrgTreeModel) => void) {
	let stack = [...tree] as OrgTreeModel[]
	while (stack.length > 0) {
		const item = stack.shift()!
		callback(item)
		if (item.children && item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
}
export function getOrgItemById(tree: OrgTreeModel[], id: string) {
	let stack = [...tree] as OrgTreeModel[]
	while (stack.length > 0) {
		const item = stack.shift()!
		if (item.id === id) {
			return item
		}
		if (item.children && item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return null
}
export const flatOrgTreeToMap = (tree: OrgTreeModel[]) => {
	let stack = [...tree] as OrgTreeModel[]
	let result = {} as { [key: string]: OrgTreeModel }
	while (stack.length !== 0) {
		const item = stack.shift()!
		result[item.id] = item
		if (item.children && item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return result
}
function removeUser(
	treeMap: {
		[key: string]: OrgTreeModel
	},
	id: string,
	removeMt: OrgModel
) {
	if (id !== '') {
		let current = treeMap[id]
		if (current) {
			current.org.userSize = current.org.userSize - 1 < 0 ? 0 : current.org.userSize - 1
			current.org.users = current.org.users.filter((x) => x.mtKey !== removeMt.mtKey)
			if (removeMt.mcc.status.isOnline) {
				current.org.userOnlineSize = current.org.userOnlineSize - 1 < 0 ? 0 : current.org.userOnlineSize - 1
			}
			removeUser(treeMap, current.parentId, removeMt)
		}
	}
}
export const removeUserInOrgTree = (tree: OrgTreeModel[], removeMt: OrgModel) => {
	const treeMap = flatOrgTreeToMap(tree)
	let parent = null
	let stack = [...tree] as OrgTreeModel[]
	while (stack.length > 0) {
		const item = stack.shift()!
		let mtKeys = item.org.users.map((x) => x.mtKey)
		if (mtKeys.includes(removeMt.mtKey)) {
			parent = item
			break
		}
		if (item.children && item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	//改变在线和人数值
	if (parent) {
		removeUser(treeMap, parent.id, removeMt)
	}
	//0/0 的数据直接删除
	getItemInOrgTree(tree, (item) => {
		if (item.children) {
			item.children = item.children.filter((x) => x.org.userSize !== 0)
		}
	})
	return tree
}
function updateUser(
	treeMap: {
		[key: string]: OrgTreeModel
	},
	id: string,
	updateMt: IOrgTreeUpdateMt
) {
	if (id !== '') {
		let current = treeMap[id]
		if (current) {
			if (updateMt.prevIsOnline) {
				current.org.userOnlineSize = Math.max(current.org.userOnlineSize - 1, 0)
			} else {
				current.org.userOnlineSize = Math.min(current.org.userOnlineSize + 1, current.org.userSize)
			}
			updateUser(treeMap, current.parentId, updateMt)
		}
	}
}
export const updateUserInOrgTree = (tree: OrgTreeModel[], updateMts: IOrgTreeUpdateMt[]) => {
	const treeMap = flatOrgTreeToMap(tree)
	for (const updateMt of updateMts) {
		if (updateMt.prevIsOnline === updateMt.nowIsOnline) {
			continue
		}
		let parent = null
		let stack = [...tree] as OrgTreeModel[]
		while (stack.length > 0) {
			const item = stack.shift()!
			let mt = item.org.users.find((x) => x.mtKey === updateMt.mtKey)
			if (mt && !!mt.online !== updateMt.nowIsOnline) {
				parent = item
				mt.online = +updateMt.nowIsOnline
				break
			}
			if (item.children && item.children.length > 0) {
				for (let i = item.children.length - 1; i >= 0; i--) {
					stack.unshift(item.children[i])
				}
			}
		}
		//改变在线值
		if (parent) {
			updateUser(treeMap, parent.id, updateMt)
		}
	}
	return tree
}
